//公共js，主要做表单验证，以及基本方法封装

const utils = {
	isNullOrEmpty: function(value) {
		//是否为空
		return (value === null || value === '' || value === undefined) ? true : false;
	},
	
	trim: function(value) {
		//去空格
		return value.replace(/(^\s*)|(\s*$)/g, "");
	},
			// var reg = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
	isEmail: function(value) {
		//是否是邮箱
		return  /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/.test(value);
	},
	
	isMobile: function(value) {
		//是否为手机号
		return /^(?:13\d|14\d|15\d|16\d|17\d|18\d|19\d)\d{5}(\d{3}|\*{3})$/.test(value);
	},
	isFloat: function(value) {
		//金额，只允许保留两位小数
		return /^([0-9]*[.]?[0-9])[0-9]{0,1}$/.test(value);
	},
	isNum: function(value) {
		//是否全为数字
		return /^[0-9]+$/.test(value);
	},
	checkPwd: function(value) {
		//密码为8~20位数字和字母组合
		return /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,20}$/.test(value);
	},
	formatNum: function(num) {
		//格式化手机号码
		if (utils.isMobile(num)) {
			num = num.replace(/^(\d{3})\d{4}(\d{4})$/, '$1****$2')
		}
		return num;
	},
	rmoney: function(money) {
		//金额格式化
		return parseFloat(money).toFixed(2).toString().split('').reverse().join('').replace(/(\d{3})/g, '$1,').replace(
			/\,$/, '').split('').reverse().join('');
	},
	getTimestamp(time) { //把时间日期转成时间戳
		return (new Date(time)).getTime() / 1000
	},

	getDateDiff: function(dateTimeStamp) {
		dateTimeStamp  = dateTimeStamp* 1000
		let result = '';
		 var minute = 1000 * 60;      //把分，时，天，周，半个月，一个月用毫秒表示
		    var hour = minute * 60;
		    var day = hour * 24;
		    var week = day * 7;
		    var halfamonth = day * 15;
		    var month = day * 30;
		    var now = new Date().getTime();   //获取当前时间毫秒
		    // console.log(now)
		    var diffValue = now - dateTimeStamp;//时间差
		
		    if(diffValue < 0){
		        return;
		    }
		    var minC = diffValue/minute;  //计算时间差的分，时，天，周，月
		    var hourC = diffValue/hour;
		    var dayC = diffValue/day;
		    var weekC = diffValue/week;
		    var monthC = diffValue/month;
		    if(monthC >= 1 && monthC <= 3){
		        result = " " + parseInt(monthC) + "月前"
		    }else if(weekC >= 1 && weekC <= 3){
		        result = " " + parseInt(weekC) + "周前"
		    }else if(dayC >= 1 && dayC <= 6){
		        result = " " + parseInt(dayC) + "天前"
		    }else if(hourC >= 1 && hourC <= 23){
		        result = " " + parseInt(hourC) + "小时前"
		    }else if(minC >= 1 && minC <= 59){
		        result =" " + parseInt(minC) + "分钟前"
		    }else if(diffValue >= 0 && diffValue <= minute){
		        result = "刚刚"
		    }else {
		        var datetime = new Date();
		        datetime.setTime(dateTimeStamp);
		        var Nyear = datetime.getFullYear();
		        var Nmonth = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
		        var Ndate = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
		        var Nhour = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours();
		        var Nminute = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
		        var Nsecond = datetime.getSeconds() < 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
		        result = Nyear + "-" + Nmonth + "-" + Ndate
		    }
		    return result;
		// var minute = 1000 * 60;
		// 	var hour = minute * 60;
		// 	var day = hour * 24;
		// 	var halfamonth = day * 15;
		// 	var month = day * 30;
		// 	var now = new Date().getTime();
		// 	var result ;
		// 	var diffValue = now/1000 - valueTime/1000;
			
		// 	if(diffValue < 0){return;}
		// 	var monthC =diffValue/month;
		// 	var weekC =diffValue/(7*day);
		// 	var dayC =diffValue/day;
		// 	var hourC =diffValue/hour;
		// 	var minC =diffValue/minute;
		// 	if(monthC>=1){
		// 		result="" + parseInt(monthC) + "月前";
		// 	}
		// 	else if(weekC>=1){
		// 		result="" + parseInt(weekC) + "周前";
		// 	}
		// 	else if(dayC>=1){
		// 		result=""+ parseInt(dayC) +"天前";
		// 	}
		// 	else if(hourC>=1){
		// 		result=""+ parseInt(hourC) +"小时前";
		// 	}
		// 	else if(minC>=1){
		// 		result=""+ parseInt(minC) +"分钟前";
		// 	}else
		// 	result="刚刚";
		// 	return result;
		
	}
	,
	getTimeText:function(argument) {
		let  strTime = this.formatDate('y年m月d日 h:i:s',argument);
		var timeS =  this.getTimeText(strTime);
		var todayT = ''; //
		var yestodayT = '';
		var timeCha = this.getTimeS(timeS);
		timeS = timeS.slice(-8);
		todayT = new Date().getHours()*60*60*1000 + new Date().getMinutes()*60*1000 + new Date().getSeconds()*1000;
		yestodayT = todayT + 24*60*60*1000;
		if(timeCha > yestodayT) {					 
			return argument.slice(5,11);
		}
		if(timeCha > todayT && timeCha < yestodayT) {
			return timeS.slice(0,2)>12?'昨天'+(timeS.slice(0,2)==12 ? 12 : timeS.slice(0,2) - 12)+timeS.slice(2,5):'昨天'+timeS.slice(0,5);
		}
		if(timeCha < todayT) {
			return timeS.slice(0,2)>=12?'下午'+(timeS.slice(0,2)==12 ? 12 : timeS.slice(0,2) - 12)+timeS.slice(2,5):'上午'+timeS.slice(0,5);
		}
	},
	getTimeS: function(argument) {
	     var timeS = argument;
	     timeS = timeS.replace(/[年月]/g,'/').replace(/[日]/,'');
		 
	     return new Date().getTime() - new Date(timeS).getTime() - 1000; //有一秒的误差
	 
	},
	yuanTocents: function(fee){

		return fee* 100;
	},
	centsToYuan: function(fee){
		return fee/100;
	},
	formatDate: function(formatStr, fdate) {
		//日期格式化
		if (fdate) {
			// if (~fdate.indexOf('.')) {
			// 	fdate = fdate.substring(0, fdate.indexOf('.'));
			// }
			// fdate = fdate.toString().replace('T', ' ').replace(/\-/g, '/');
			var fTime, fStr = 'ymdhis';
			if (!formatStr)
				formatStr = "y-m-d h:i:s";
			if (fdate)
				fTime = new Date(fdate * 1000);
			else
				fTime = new Date();
	
			var month = fTime.getMonth() + 1;
			var day = fTime.getDate();
			var hours = fTime.getHours();
			var minu = fTime.getMinutes();
			var second = fTime.getSeconds();
			month = month < 10 ? '0' + month : month;
			day = day < 10 ? '0' + day : day;
			hours = hours < 10 ? ('0' + hours) : hours;
			minu = minu < 10 ? '0' + minu : minu;
			second = second < 10 ? '0' + second : second;			
			var formatArr = [
				fTime.getFullYear().toString(),
				month.toString(),
				day.toString(),
				hours.toString(),
				minu.toString(),
				second.toString()
			]

			for (var i = 0; i < formatArr.length; i++) {
				formatStr = formatStr.replace(fStr.charAt(i), formatArr[i]);
			}
			return formatStr;
		} else {
			return "";
		}
	}
}

module.exports = {
	isNullOrEmpty: utils.isNullOrEmpty,
	trim: utils.trim,
	isMobile: utils.isMobile,
	isFloat: utils.isFloat,
	isNum: utils.isNum,
	checkPwd: utils.checkPwd,
	formatNum:utils.formatNum,
	rmoney: utils.rmoney,
	formatDate: utils.formatDate,
	getTimestamp: utils.getTimestamp,
	getTimeText: utils.getTimeText,
	centsToYuan: utils.centsToYuan,
	yuanTocents: utils.yuanTocents,
	isEmail: utils.isEmail,
	getDateDiff:utils.getDateDiff
}
